package edu.bw.mapper;

import edu.bw.po.Product;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface ProductMapper {

    /**
     * keyProperty: 主键对应的属性
     * keyColumn: 主键列
     * resultType: 主键的java类型
     * statement: 执行的语句
     * before: true:在插入语句之前查询主键 false:在插入语句之后查询主键
     *
     * @param product
     */
    @SelectKey(keyProperty = "id", keyColumn = "id", resultType = int.class,
            statement = "SELECT LAST_INSERT_ID()", before = false)
    @Insert("INSERT INTO mybatis02.product(name,price,detail,pic,createtime) VALUES " +
            "(#{name},#{price},#{detail},#{pic},#{createtime})")
    void saveProduct(Product product);

    @Update("UPDATE mybatis02.product SET name=#{name},price=#{price},detail=#{detail},pic=#{pic}," +
            "createtime=#{createtime} Where id=#{id}")
    void updateProduct(Product product);

    @Delete("DELETE FROM mybatis02.product WHERE id=#{id}")
    void deleteProduct(int id);

    @Select("SELECT * FROM mybatis02.product")
    List<Product> findAll();

    /**
     * //@Results 注解相当于映射文件中的 <resultMap></resultMap>
     * //@Results中的id属性 相当于映射文件中的 <resultMap id ="xxx"></resultMap>
     * //@Result 注解相当于映射文件中 <result></result>
     * //@Result 注解中的id属性 相当于映射文件中的 <id></id>
     *
     * @param id
     * @return
     */
    @Results({
            @Result(column = "pid", property = "id", id = true),
            @Result(column = "pname",property = "name")
    })
    @Select("SELECT id AS pid, name AS pname, price, detail, pic , createtime FROM mybatis02.product WHERE id=#{id}")
    Product findById(int id);
}
